#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
using UnityEditor;

namespace GameDesigner
{
    public class AddPopupWindow : AdvancedDropdown
    {
        private readonly string rootName;
        private readonly List<string> options;
        private readonly string iconName;
        private Action<string> onSelect;

        public AddPopupWindow(string rootName, List<string> options, AdvancedDropdownState state, string iconName = "d_AnimationClip Icon") : base(state)
        {
            this.rootName = rootName;
            this.options = options;
            this.iconName = iconName;
        }

        public void Init(Rect rect, Action<string> onSelect)
        {
            this.onSelect = onSelect;
            Show(rect);
        }

        protected override AdvancedDropdownItem BuildRoot()
        {
            var root = new AdvancedDropdownItem(rootName);
            foreach (var option in options)
            {
                var dropdownItem = new AdvancedDropdownItem(option)
                {
                    icon = EditorGUIUtility.IconContent(iconName).image as Texture2D
                };
                root.AddChild(dropdownItem);
            }
            return root;
        }

        protected override void ItemSelected(AdvancedDropdownItem item)
        {
            onSelect?.Invoke(item.name);
        }
    }
}
#endif